//
//  XLNetworking.h
//  MPTinyAppLearn
//
//  Created by xl on 2019/10/29.
//  Copyright © 2019 xl. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "XLNetworkingConfig.h"


typedef NS_ENUM(NSUInteger, XLResponseType) {
    XLResponseTypeJSON = 1,         // 默认
    XLResponseTypeXML  = 2,         // XML
    XLResponseTypeData = 3          // 特殊情况下，一转换服务器就无法识别的，默认会尝试转换成JSON，若失败则需要自己去转换
};

typedef NS_ENUM(NSUInteger, XLRequestType) {
    XLRequestTypeJSON       = 1,    // 默认
    XLRequestTypePlainText  = 2     // 普通text/html
};

typedef NSURLSessionTask XLURLSessionTask;

typedef void(^XLResponseSuccess)(XLServerRequestsStatus status, XLNetworkReachabilityStatus reachability,  id response);

typedef void(^XLResponseFail)(XLServerRequestsStatus status, XLNetworkReachabilityStatus reachability, id response, NSError *error);

typedef void(^XLLoadProgress)(int_fast64_t bytesRead, int_fast64_t totalBytesRead);


@interface XLNetworking : NSObject

+ (NSTimeInterval)configTimeout:(NSTimeInterval)timeout;
/**
 *  配置公共的请求头，用于区分请求来源，需要与服务器约定好
 *  @param httpHeaders      如@{"client" : "iOS"}
 */
+ (void)configCommonHttpHeaders:(NSDictionary *)httpHeaders;

/**
 *  GET请求接口
 *  @param url          访问地址路径，如/user/index/login
 *  @param params       需要传的参数，如@{@"user_id" :@(80011)}
 *  @param success      接口请求响应成功回调
 *  @param fail         接口请求响应失败回调
 *  @return             NSURLSessionTask
 */
+ (XLURLSessionTask *)getWithUrl:(NSString *)url
                          params:(NSDictionary *)params
                         success:(XLResponseSuccess)success
                            fail:(XLResponseFail)fail;

/**
 *  GET请求接口
 *  @param url          访问地址路径，如/user/index/login
 *  @param params       需要传的参数，如@{@"user_id" :@(80011)}
 *  @param progress     进度回调，
 *  @param success      接口请求响应成功回调
 *  @param fail         接口请求响应失败回调
 *  @return             NSURLSessionTask
 */
+ (XLURLSessionTask *)getWithUrl:(NSString *)url
                          params:(NSDictionary *)params
                        progress:(XLLoadProgress)progress
                         success:(XLResponseSuccess)success
                            fail:(XLResponseFail)fail;

/**
 *  POST请求接口
 *  @param url          访问地址路径，如/user/index/login
 *  @param params       需要传的参数，如@{@"user_id" :@(80011)}
 *  @param success      接口请求响应成功回调
 *  @param fail         接口请求响应失败回调
 *  @return             NSURLSessionTask
 */
+ (XLURLSessionTask *)postWithUrl:(NSString *)url
                           params:(NSDictionary *)params
                          success:(XLResponseSuccess)success
                             fail:(XLResponseFail)fail;

/**
 *  POST请求接口
 *  @param url          访问地址路径，如/user/index/login
 *  @param params       需要传的参数，如@{@"user_id" :@(80011)}
 *  @param progress     进度回调，
 *  @param success      接口请求响应成功回调
 *  @param fail         接口请求响应失败回调
 *  @return             NSURLSessionTask
 */
+ (XLURLSessionTask *)postWithUrl:(NSString *)url
                           params:(NSDictionary *)params
                         progress:(XLLoadProgress)progress
                          success:(XLResponseSuccess)success
                             fail:(XLResponseFail)fail;

/**
 *  图片上传接口
 *  @param image        图片对象
 *  @param url          上传图片路径，如/user/images
 *  @param fileName     文件名字，默认为当前时间yyyyMMddHHmmss.jpg
 *  @param name         约定关联名称，如image
 *  @param parameters   需要传的参数，如@{@"user_id" :@(80011)}
 *  @param progress     上传进度回调
 *  @param success      上传成功回调
 *  @param fail         上传失败回调
 *  @return             NSURLSessionTask
 */
+ (XLURLSessionTask *)uploadWithImage:(UIImage *)image
                                  url:(NSString *)url
                             fileName:(NSString *)fileName
                                 name:(NSString *)name
                           parameters:(NSDictionary *)parameters
                             progress:(XLLoadProgress)progress
                              success:(XLResponseSuccess)success
                                 fail:(XLResponseFail)fail;

/** 更新 app token header **/
+ (void)updateCustomHeaderDicWithToken:(NSString *)token;

//上传认证图片
+ (void)uploadCertifyImageWithUrl:(NSString *)url
                           params:(NSDictionary *)params
                       stuPicArrs:(NSArray *)picArrs
                      idCardFront:(UIImage *)idCardFront
                     idCardBehind:(UIImage *)idCardBehind
                         mimeType:(NSString *)mimeType
                         progress:(XLLoadProgress)progress
                          success:(XLResponseSuccess)success
                             fail:(XLResponseFail)fail;


@end

